﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.Entity;
namespace EHTWebsiteProject.Code.BLL
{
    public class OrderDetaiProvider
    {
        #region Quản lý Order

        public static void UIOrderDetal(OrderDetail theOrderDetail, Boolean isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUOrderDetail";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", theOrderDetail.ID));
            command.Parameters.Add(new SqlParameter("@OrderID", theOrderDetail.OrderID));
            command.Parameters.Add(new SqlParameter("@SanPhamID", theOrderDetail.SanPhamID));
            command.Parameters.Add(new SqlParameter("@UnitPrice", theOrderDetail.UnitPrice));
            command.Parameters.Add(new SqlParameter("@Quantity", theOrderDetail.Quantity));
            command.Parameters.Add(new SqlParameter("@Discount", theOrderDetail.Discount));
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void DeleteOrderDetailOderByID(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteOrderDetailByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));
            DataProvider.ExecuteNoneQuery(command);
        }
        public static void DeleteOrderDetailOderByOrderID(Int32 OrderID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteOrderDetailByOrderID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@OrderID", OrderID));
            DataProvider.ExecuteNoneQuery(command);
        }
        public static OrderDetail GetOrderDetailByID(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetOrderDetailByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            OrderDetail theOrderDetail = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theOrderDetail = new OrderDetail();
                theOrderDetail.Discount = float.Parse(ds.Tables[0].Rows[0]["Discount"].ToString());
                theOrderDetail.ID = Int32.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                theOrderDetail.OrderID = Int32.Parse(ds.Tables[0].Rows[0]["OrderID"].ToString());
                theOrderDetail.SanPhamID = Int32.Parse(ds.Tables[0].Rows[0]["ProductID"].ToString());
                theOrderDetail.Quantity = float.Parse(ds.Tables[0].Rows[0]["Quantity"].ToString());
                theOrderDetail.UnitPrice = float.Parse(ds.Tables[0].Rows[0]["UnitPrice"].ToString());
            }

            return theOrderDetail;
        }

        public static DataSet GetOrderDetailByOrderID(Int32 orderID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetOrderDetailByOrderID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@OrderID", orderID));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }

        #endregion
    }
}